package fr.auzaiphe.admin.view;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;

import fr.auzaiphe.admin.constantes.ConstantesAdmin;
import fr.auzaiphe.admin.controller.AdminController;

/**
 * Classe abstraite pour la modification et suppression d'un champ lors d'un clic dessus
 * @author Anthony REY
 * @since 09/05/2012
 * @abstract
 */
public abstract class SelectionTableResultat implements MouseListener
{
	private JTable table;
	protected AdminController controller;
	private ListSelectionModel lsm;
	private int selectedRow;
	private JPopupMenu menu;
	private JMenuItem menuItem;

	/**
	 * Surcharge du constructeur de liste
	 * @param pTable La table
	 * @param pController Le controlleur
	 */
	public SelectionTableResultat(JTable pTable, AdminController pController)
	{
		super();
		this.table = pTable;
		this.controller = pController;
	}

    /**
     * Récupération des informations du champ
     */
	protected abstract void recuperation();
	protected abstract void suppression();

	/**
	 * Listeners
	 */
	@Override
	public void mouseClicked(MouseEvent pE)
	{
		if ( pE.getClickCount() == 2 )
		{
			JTable target = (JTable) pE.getSource();
			this.selectedRow= target.getSelectedRow();

			/*
	    	 * Vérification de la ligne sélectionnée
	    	 */
			if(this.selectedRow >= 0)
			{
	            this.recuperation();
	            this.table.getSelectionModel().removeSelectionInterval(this.selectedRow, 0);
			}
		}
	}

	@Override
	public void mouseEntered(MouseEvent pE) {}

	@Override
	public void mouseExited(MouseEvent pE) {}

	@Override
	public void mousePressed(MouseEvent pE)
	{
        if (pE.isPopupTrigger())
        {
            this.doPop(pE);
        }
	}

	@Override
	public void mouseReleased(MouseEvent pE)
	{
        if (pE.isPopupTrigger())
        {
            this.doPop(pE);
        }
	}

	/**
	 * Menu du clic droit
	 * @param pE L'évènement
	 */
    private void doPop(MouseEvent pE)
    {
    	Point p = pE.getPoint();
    	this.selectedRow = this.table.rowAtPoint( p );

    	/*
    	 * Vérification de la ligne sélectionnée
    	 */
    	if(this.selectedRow >= 0)
    	{
	        this.menu = new JPopupMenu();

	        /*
	         * Édition
	         */
	        this.menuItem = new JMenuItem(ConstantesAdmin.MENU_ACTION_TABLEAU[0]);
	        this.menu.add(this.menuItem);
	        this.menuItem.addActionListener
	        (
	        		new ActionListener()
	        		{
						@Override
						public void actionPerformed(ActionEvent arg0)
						{
							SelectionTableResultat.this.recuperation();
							SelectionTableResultat.this.getTable().getSelectionModel().removeSelectionInterval(SelectionTableResultat.this.getSelectedRow(), 0);
						}
					}
	        );

	        /*
	         * Suppression
	         */
	        this.menuItem = new JMenuItem(ConstantesAdmin.MENU_ACTION_TABLEAU[1]);
	        this.menuItem.addActionListener
	        (
        		new ActionListener()
        		{
					@Override
					public void actionPerformed(ActionEvent arg0)
					{
						System.out.println("Suppression");
						SelectionTableResultat.this.suppression();
					}
				}
	        );
	        this.menu.add(this.menuItem);
	        this.menu.show(pE.getComponent(), pE.getX(), pE.getY());
    	}
    }

	/*
	 * Getters
	 */
	public JTable getTable() {
		return this.table;
	}

	public AdminController getController()
	{
		return this.controller;
	}

	public ListSelectionModel getLsm() {
		return this.lsm;
	}

	public int getSelectedRow() {
		return this.selectedRow;
	}
}